package com.twu.biblioteca;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;


public class BibliotecaApp {

    Collection bookCollection;
    Collection movieCollection;
    UserCollection userCollection;
    PrintStream printStream;
    InputStream inputStream;

    BibliotecaApp(Collection bookCollection, Collection movieCollection, UserCollection userCollection, PrintStream printStream, InputStream inputStream) {
        this.bookCollection = bookCollection;
        this.movieCollection = movieCollection;
        this.userCollection = userCollection;
        this.printStream = printStream;
        this.inputStream = inputStream;
    }

    public void run(User currentUser) throws Exception {
        Scanner scanner = new Scanner(inputStream);
        scanner.useDelimiter("\n");
        printStream.println("Welcome to Biblioteca");
        Menu.printMenu(printStream);
        while (scanner.hasNext()) {
            String option = scanner.next();
            Menu menu = Menu.find(option);
            ViewState viewState = menu.execute(scanner, printStream, inputStream, bookCollection, movieCollection, userCollection, currentUser);
            if (!viewState.continueRunning) {
                break;
            }
            currentUser = viewState.currentUser;
            Menu.printMenu(printStream);
        }
    }
}
